home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac Format 1994 October
/
Macformat17.cdr
/
Shareware City
/
Developers
/
CPreferences Folder
/
CPreferences.cp
< prev
next >
Wrap
Text File
|
1994-01-20
|
3KB
|
119 lines
// CPreferences.cp 1.0
// Created by Richard A. Bray
// ©1994 Compu-Phobic, Inc.
#include "CPreferences.h"
#include "LFile.h"
#include <Folders.h>
//----------------------------------
// CPreferences default constructor
// This will create a preference file named "Default Prefs" with
// a creator of "????" in the Preferences folder.
//----------------------------------
CPreferences::CPreferences()
{
FSSpec *spec, theSpec;
OSErr fsErr;
Handle prefs;
mCreator = '????';
spec = &theSpec;
fsErr = FindFolder(kOnSystemDisk, 'pref',
kCreateFolder, &(spec->vRefNum), &(spec->parID) );
fsErr = FSMakeFSSpec(spec->vRefNum, spec->parID, "\pDefault Prefs", spec);
prefFile = new LFile(theSpec);
if(fsErr == fnfErr) {
CreateDefaultPreference();
}
}
//----------------------------------
// CPreferences constructor
// This will create a preference file named 'inName' with
// a creator of 'inCreator' in the Preferences folder.
//----------------------------------
CPreferences::CPreferences(OSType inCreator, ConstStr255Param inName)
{
FSSpec *spec, theSpec;
OSErr fsErr;
Handle prefs;
mCreator = inCreator;
spec = &theSpec;
fsErr = FindFolder(kOnSystemDisk, 'pref',
kCreateFolder, &(spec->vRefNum), &(spec->parID) );
fsErr = FSMakeFSSpec(spec->vRefNum, spec->parID, inName, spec);
prefFile = new LFile(theSpec);
if(fsErr == fnfErr) {
CreateDefaultPreference();
}
}
//----------------------------------
// CPreferences default destructor
// delete file object and any other handles.
//----------------------------------
CPreferences::~CPreferences(void)
{
delete prefFile;
}
//----------------------------------
// LoadPreferences
// Load preferences data from file.
//----------------------------------
void CPreferences::LoadPreferences()
{
Int32 count = sizeof(Preference);
Handle prefs;
prefFile->OpenDataFork(fsCurPerm);
prefs = prefFile->ReadDataFork();
prefFile->CloseDataFork();
// set mPrefs to point to handle
mPrefs = **(Preference**)prefs;
DisposHandle(prefs);
}
//----------------------------------
// SavePreferences
// Write data to file
//----------------------------------
void CPreferences::SavePreferences()
{
prefFile->OpenDataFork(fsCurPerm);
prefFile->WriteDataFork(&mPrefs, sizeof(Preference));
prefFile->CloseDataFork();
}
//----------------------------------
// CreateDefaultPreference
// Create preference file with default values from
// 'pref' resource in application.
//----------------------------------
void
CPreferences::CreateDefaultPreference(void)
{
Handle prefs;
// If the preference file does not exist, then load the
// 'pref' resource in the application's resource fork.
// Use that as the default data for a new preference file.
prefFile->CreateNewFile(mCreator, 'pref', -1);
prefs = GetResource('pref', 128);
mPrefs = **(Preference**)prefs;
ReleaseResource(prefs);
SavePreferences();
}
//----------------------------------
// GetPreferences
// Load preferences data from file.
//----------------------------------
void CPreferences::GetPreferences(Preference &ioPrefs)
{
ioPrefs = mPrefs;
}